var RegisterHandler = function () {
}
RegisterHandler.prototype.listSendCaptchaEvent = function () {
    var callback = function (event) {
        event.preventDefault()
        var email = $("input[name='email']").val()
        var reg = /^\w+((.\w+)|(-\w+))@([A-Za-z0-9]+)((.[A-Za-z0-9]+)+)$/
        var $this = $(this)
        if (!email || !reg.test(email)) {
            alert("请输入正确邮箱格式!")
            return
        }
        zlajax.get({
            'url': '/email/captcha?email=' + email,
            'success': function (result) {
                if (result['code'] == 200) {
                    console.log('邮件发送成功')
                    $this.off('click')
                    $this.attr('disabled', 'disabled')
                    var countdown = 6
                    var interval = setInterval(function () {
                        if (countdown > 0) {
                            $this.text(countdown + '秒后重试')
                        } else {
                            $this.text('发送邮箱验证码')
                            $this.removeAttr('disabled')
                            $this.on('click', callback)
                            clearInterval(interval)
                        }
                        countdown -= 1
                    }, 1000)
                } else {
                    var message = result['message']
                    alert(message)
                }
            }
        })
    }
    $('#email-captcha-btn').on('click', callback)
}

RegisterHandler.prototype.listenGraphCaptchaEvent = function () {
    $('#captcha-img').on('click', function (event) {
        $this = $(this)
        var src = $this.attr('src')
        let new_src = zlparam.setParam(src, 'sign', Math.random())
        $this.attr('src', new_src)
    })
}

RegisterHandler.prototype.listenSubmitEvent = function () {
    $('#submit-btn').on('click', function (event) {
        event.preventDefault()
        var email = $("input[name='email']").val()
        var email_captcha = $("input[name='email_captcha']").val()
        var username = $("input[name='username']").val()
        var password = $("input[name='password']").val()
        var repeat_password = $("input[name='repeat_password']").val()
        var graph_captcha = $("input[name='graph_captcha']").val()
        // 以后验证
        zlajax.post({
            url: '/register',
            data: {
                'email': email,
                'email_captcha': email_captcha,
                'username': username,
                'password': password,
                'repeat_password': repeat_password,
                'graph_captcha': graph_captcha
            },
            success: function (result) {
                if (result['code'] == 200) {
                    window.location = '/login'
                } else {
                    alert(result['message'])
                }
            }
        })
    })
}

RegisterHandler.prototype.run = function () {
    this.listSendCaptchaEvent()
    this.listenGraphCaptchaEvent()
    this.listenSubmitEvent()
}

$(function () {
    var handler = new RegisterHandler()
    handler.run()
})